import axios from 'axios'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'

const request = axios.create({
  timeout: 5000,
  // baseURL: 'http://www.foxchen.top:3001/'
  // baseURL: 'http://192.168.0.104:3000/'
  baseURL: 'http://localhost:3000/',
})

request.interceptors.request.use((config) => {
  let cookie = localStorage.getItem('token') || ''
  // console.log(config)
  NProgress.start()
  if (config.method === 'get') {
    if (config.params) {
      config.params.cookie = cookie
      config.params.withCredentials = true
    } else {
      config.params = {
        cookie,
      }
    }
  } else {
    let data = config.data || config.params || {}
    data.withCredentials = true
    data.cookie = cookie
  }

  return config
})

request.interceptors.response.use(
  (use) => {
    NProgress.done()
    return use
  },
  (err) => {
    NProgress.done()
    return err
  }
)

export default request
